﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX;

namespace EdytorWF.Engine
{
    class BaseCamera
    {
        public Vector4 Position = new Vector4(10, 10, 0, 1);
        public Vector3 Forward = new Vector3(0, 0, 1);
        public Vector3 Right = new Vector3(1, 0, 0);
        public Vector3 Up = new Vector3(0, 1, 0);
        Matrix baseMatrix = new Matrix();
        public Matrix translateMatrix = new Matrix();
        Matrix scaleMatrix = new Matrix();
        Matrix rotateXMatrix = new Matrix();
        Matrix rotateYMatrix = new Matrix();
        Matrix rotateZMatrix = new Matrix();

        public Vector4 translateVector = new Vector4(0,0,0,1); // współrzędne jednorodne
        public Vector4 scaleVector = new Vector4(0,0,0,1);
        public Vector4 rotateVector = new Vector4(0,0,0,1);

        public BaseCamera()
        {
            InitTranslateMatrix();
            InitScaleMatrix();
            InitRotateXMatrix();
            InitRotateYMatrix();
            InitRotateZMatrix();
        }

        private void InitTranslateMatrix()
        {
            translateMatrix.M11 = 1; translateMatrix.M12 = 0; translateMatrix.M13 = 0; translateMatrix.M14 = 0;                                                 //1,0,0,0
            translateMatrix.M21 = 0; translateMatrix.M22 = 1; translateMatrix.M23 = 0; translateMatrix.M24 = 0;                                                 //0,1,0,0
            translateMatrix.M31 = 0; translateMatrix.M32 = 0; translateMatrix.M33 = 1; translateMatrix.M34 = 0;                                                 //0,0,1,0
            translateMatrix.M41 = translateVector.X; translateMatrix.M42 = translateVector.Y; translateMatrix.M43 = translateVector.Z; translateMatrix.M44 = 1; //x,y,z,1
        }

        private void InitScaleMatrix()
        {
            scaleMatrix.M11 = scaleVector.X; scaleMatrix.M12 = 0; scaleMatrix.M13 = 0; scaleMatrix.M14 = 0;                                                     //x,0,0,0
            scaleMatrix.M21 = 0; scaleMatrix.M22 = scaleVector.Y; scaleMatrix.M23 = 0; scaleMatrix.M24 = 0;                                                     //0,y,0,0
            scaleMatrix.M31 = 0; scaleMatrix.M32 = 0; scaleMatrix.M33 = scaleVector.Z; scaleMatrix.M34 = 0;                                                     //0,0,z,0
            scaleMatrix.M41 = 0; scaleMatrix.M42 = 0; scaleMatrix.M43 = 0; scaleMatrix.M44 = 1;                                                                 //0,0,0,1
        }

        private void InitRotateXMatrix()
        {
            rotateXMatrix.M11 = 1; rotateXMatrix.M12 = 0; rotateXMatrix.M13 = 0; rotateXMatrix.M14 = 0;                                                             //1,   0,  0,0
            rotateXMatrix.M21 = 0; rotateXMatrix.M22 = (float)Math.Cos(rotateVector.X); rotateXMatrix.M23 = (float)Math.Sin(rotateVector.X); rotateXMatrix.M24 = 0; //0, cos,sin,0
            rotateXMatrix.M31 = 0; rotateXMatrix.M32 = -(float)Math.Sin(rotateVector.X); rotateXMatrix.M33 = (float)Math.Cos(rotateVector.X); rotateXMatrix.M34 = 0;//0,-sin,cos,0
            rotateXMatrix.M41 = 0; rotateXMatrix.M42 = 0; rotateXMatrix.M43 = 0; rotateXMatrix.M44 = 1;                                                             //0,   0,  0,1
        }

        private void InitRotateYMatrix()
        {
            rotateYMatrix.M11 = (float)Math.Cos(rotateVector.Y); rotateYMatrix.M12 = 0; rotateYMatrix.M13 = -(float)Math.Sin(rotateVector.Y); rotateYMatrix.M14 = 0;    //cos,0,-sin,0
            rotateYMatrix.M21 = 0; rotateYMatrix.M22 = 1; rotateYMatrix.M23 = 0; rotateYMatrix.M24 = 0;                                                                 //0,  1,   0,0
            rotateYMatrix.M31 = (float)Math.Sin(rotateVector.Y); rotateYMatrix.M32 = 0; rotateYMatrix.M33 = (float)Math.Cos(rotateVector.Y); rotateYMatrix.M34 = 0;     //sin,0, cos,0
            rotateYMatrix.M41 = 0; rotateYMatrix.M42 = 0; rotateYMatrix.M43 = 0; rotateYMatrix.M44 = 1;                                                                 //0,0,     0,1    
        }

        private void InitRotateZMatrix()
        {
            rotateYMatrix.M11 = (float)Math.Cos(rotateVector.Z); rotateYMatrix.M12 = (float)Math.Sin(rotateVector.Z); rotateYMatrix.M13 = 0; rotateYMatrix.M14 = 0;     //cos, sin,0,0
            rotateYMatrix.M21 = -(float)Math.Sin(rotateVector.Z); rotateYMatrix.M22 = (float)Math.Cos(rotateVector.Z); rotateYMatrix.M23 = 0; rotateYMatrix.M24 = 0;    //-sin,cos,0,0
            rotateYMatrix.M31 = 0; rotateYMatrix.M32 = 0; rotateYMatrix.M33 = 1; rotateYMatrix.M34 = 0;                                                                 //0,     0,1,0
            rotateYMatrix.M41 = 0; rotateYMatrix.M42 = 0; rotateYMatrix.M43 = 0; rotateYMatrix.M44 = 1;                                                                 //0,     0,0,1
        }

        private void SetTranslateMatrix()
        {
            translateMatrix.M41 = translateVector.X; 
            translateMatrix.M42 = translateVector.Y; 
            translateMatrix.M43 = translateVector.Z; 
            translateMatrix.M44 = 1;
        }
        public void Translate(float tx, float ty, float tz)
        {
            translateVector = new Vector4(Right.X * tx + Up.X * ty + Forward.X * tz,
                Right.Y * tx + Up.Y * ty + Forward.Y * tz, 
                Right.Z * tz + Up.Z * ty + Forward.Z * tz, 
                1);

            SetTranslateMatrix();

            //Position.Transform(translateMatrix);
        }
    }
}
